using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{
    [DataObject]
    public class ExpOppBLL
    {
        private ExpOpprtintyTableAdapter _expoppAdapter = null;

        protected ExpOpprtintyTableAdapter ExpOppAdapter
        {
            get
            {
                if (_expoppAdapter == null)
                {
                    _expoppAdapter = new ExpOpprtintyTableAdapter();
                }
                return _expoppAdapter;
            }
        }

        #region SelectExportOppData

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.ExpOpprtintyDataTable GetAllExportOpp()
        {
            return ExpOppAdapter.GetExpOpprtintyRowRankData();

        }


        public int GetTotalNumberOfExportOpp()
        {
            Nullable<int> totalNull = (Nullable<int>)ExpOppAdapter.TotalNumberOfExpOpprtinty();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfExportOpp(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfExportOpp();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)ExpOppAdapter.
                TotalNumberOfExpOpprtintyByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }
        

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetExportOppByExpOppID(int Ex_oprtid)
        {
            if (Ex_oprtid <= 0)
            {
                return ExpOppAdapter.GetExpOpprtintyRowRankData();
            }
            else
            {
                return ExpOppAdapter.GetExpOpprtintyDataByExoprtId(Ex_oprtid);
            }
        }

        // To Get all Top 10 Export Data for the first Page//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetTopExportOppDataRank()
        {
            return ExpOppAdapter.GetExpOpprtintyDataTopRank();
        }

        // Get Export by Rank ID //
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetExportOppDataByRank(int RowRank)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByRowRank(RowRank);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetAllExpOpprtintyByCityNmEn(string City_nm)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByCityNmEn(City_nm);

            //if (City_nm )
            //{
            //    return ExpOppAdapter.GetAllExportOpp();
            //}
            //else
            //{
            //    return ExpOppAdapter.GetExpOpprtintyDataByCityNmEn(City_nm);
            //}
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable 
            GetExpOpprtintyByCityIDYearMonth(int cityId, int year, int month)
        {
            return ExpOppAdapter.GetExpOpptintyDataByCityIdYearMonth(cityId,
                year, month);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable
            GetExpOpprtintyByyExpOppNmArFrstLetter(string exOppName, int TopCount)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByExpOppNmArFrstLetter(exOppName, TopCount);
        }
 


        //get all tenders by countrynameEN with stored proceduers//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetSP_AllExportOppByCountry(string CountryNameEN)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByCuntryId(CountryNameEN);
            //if (CountryNameEN == null)
            //{
            //    return ExpOppAdapter.GetAllExportOpp();
            //}
            //else
            //{
            //    return ExpOppAdapter.GetAllExportOppByCountryDataByRankID(CountryNameEN);
            //}
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetAllExpOpprtintyByCountryName(string CountryNameEN)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByCountryNmEn(CountryNameEN);
            //if (CountryNameEN == null)
            //{
            //    return ExpOppAdapter.GetAllExportOpp();
            //}
            //else
            //{
            //    return ExpOppAdapter.GetExpOpprtintyDataByCountryNmEn(CountryNameEN);
            //}

        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetExpOpprtintyByGridPagesAndFilterExpression(int startRowIndex,
            int maximumRows, string sortExpression)
        {
            return ExpOppAdapter.GetExpOpprtintyDataByGridPages(startRowIndex, maximumRows, sortExpression);
        //    EcsSiteDS.ExpOpprtintyDataTable ExpOppr = GetAllExportOpp();
        //    PagedDataSource pagedData = new PagedDataSource();
        //    pagedData.DataSource = ExpOppr.Rows;
        //    pagedData.AllowPaging = true;
        //    pagedData.CurrentPageIndex = pageIndex;
        //    pagedData.PageSize = pageSize;
        //    return pagedData;
        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.ExpOpprtintyDataTable GetExpOpprtintyByGridPagesFilerExp(int startRowIndex, int maximumRows,
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetExpOpprtintyByGridPagesAndFilterExpression(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return ExpOppAdapter.GetExpOpprtintyDataByGridPagesFilerExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }


        #endregion


        #region InsertExportOppData
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddExportOpp(string Ex_oppNameAr, DateTime Ex_oprtdate, string Ex_opprtrqunt,
            string Ex_oprtspecif, string Ex_oprtpayment, string Ex_oprtshipp,
            string Ex_oppNameEn, string Ex_oprtofficoment,
            int Wcomp_id,  int ProdCat_Id, string Ex_ProdDesc ,string Ex_Prodhowtoreg,
            string Ex_path,bool Formemberonly, bool? Publishing)
        {
            EcsSiteDS.ExpOpprtintyDataTable ExportOppTable = new EcsSiteDS.ExpOpprtintyDataTable();
            EcsSiteDS.ExpOpprtintyRow ExportOppRow = ExportOppTable.NewExpOpprtintyRow();

            if (string.IsNullOrEmpty(Ex_oppNameEn)) ExportOppRow.SetEx_oppNameEnNull(); else ExportOppRow.Ex_oppNameEn = Ex_oppNameEn;
            if (string.IsNullOrEmpty(Ex_oppNameAr)) ExportOppRow.SetEx_oppNameArNull(); else ExportOppRow.Ex_oppNameAr = Ex_oppNameAr;
            if (string.IsNullOrEmpty(Ex_ProdDesc)) ExportOppRow.SetEx_ProdDescNull(); else ExportOppRow.Ex_ProdDesc = Ex_ProdDesc;            
            if (Publishing == null) ExportOppRow.SetPublishingNull(); else ExportOppRow.Publishing = Publishing.Value;
            if (string.IsNullOrEmpty(Ex_Prodhowtoreg)) ExportOppRow.SetEx_ProdhowtoregNull(); else ExportOppRow.Ex_Prodhowtoreg = Ex_Prodhowtoreg;
            if (string.IsNullOrEmpty(Ex_oprtpayment)) ExportOppRow.SetEx_oprtpaymentNull(); else ExportOppRow.Ex_oprtpayment = Ex_oprtpayment;
            if (string.IsNullOrEmpty(Ex_oprtshipp)) ExportOppRow.SetEx_oprtshippNull(); else ExportOppRow.Ex_oprtshipp = Ex_oprtshipp;
            if (string.IsNullOrEmpty(Ex_oprtofficoment)) ExportOppRow.SetEx_oprtofficomentNull(); else ExportOppRow.Ex_oprtofficoment = Ex_oprtofficoment;            
            if (string.IsNullOrEmpty(Ex_path)) ExportOppRow.SetEx_pathNull(); else ExportOppRow.Ex_path = Ex_path;

            ExportOppRow.Ex_oprtdate = Ex_oprtdate;
            ExportOppRow.Ex_opprtrqunt = Ex_opprtrqunt;
            ExportOppRow.Ex_oprtspecif = Ex_oprtspecif;            
            ExportOppRow.Wcomp_id = Wcomp_id;
            ExportOppRow.Formemberonly = Formemberonly;
            ExportOppRow.ProdCat_Id = ProdCat_Id;
            ExportOppTable.AddExpOpprtintyRow(ExportOppRow);

            int rowsAffected = ExpOppAdapter.Update(ExportOppTable);

            #region LogActivity
            Helper.LogActivity("Insert into ExportOpp table", ActivityOperation.Inserted, true,
              new LogActivityInfo[] 
                    { 
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oppNameArColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oppNameAr),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oppNameEnColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oppNameEn),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_opprtrquntColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_opprtrqunt),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oprtdateColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oprtdate),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oprtofficomentColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oprtofficoment),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oprtpaymentColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oprtpayment),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oprtshippColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oprtshipp),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_oprtspecifColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_oprtspecif),
                    new LogActivityInfo(ExportOppTable.TableName,ExportOppTable.Ex_pathColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_path),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_ProdDescColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_ProdDesc),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Ex_ProdhowtoregColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Ex_Prodhowtoreg),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.Wcomp_idColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Wcomp_id),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.ProdCat_IdColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,ProdCat_Id),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.PublishingColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Publishing.Value),
                    new LogActivityInfo(ExportOppTable.TableName,
                    ExportOppTable.FormemberonlyColumn.ColumnName,
                        ExportOppRow.Ex_oprtid,null,Formemberonly)
                    }
              );

            #endregion

            return (rowsAffected == 1) ?  ExportOppRow.Ex_oprtid :  -1;
        }


        #endregion

        #region UpdateExportOppData
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool updateExportOpp(DateTime Ex_oprtdate, string Ex_opprtrqunt, string Ex_oprtspecif,
            string Ex_oprtpayment, string Ex_oprtshipp, string Ex_oppNameAr, string Ex_oppNameEn,
            string Ex_oprtofficoment, int Wcomp_id, bool Formemberonly, bool? Publishing, int Ex_oprtid, int ProdCat_Id, 
            string Ex_ProdDesc , string Ex_Prodhowtoreg ,string Ex_path)
        {
            EcsSiteDS.ExpOpprtintyDataTable exportTable = ExpOppAdapter.GetExpOpprtintyDataByExoprtId(Ex_oprtid);
            if (exportTable.Count == 0)
            {
                return false;

            }
            EcsSiteDS.ExpOpprtintyRow exportRow = exportTable[0];

            #region LogActivity
            Helper.LogActivity("Update to ExportOpp table", ActivityOperation.Updated, true,
              new LogActivityInfo[] 
                    { 
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oppNameArColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oppNameArNull())? exportRow.Ex_oppNameAr : null),Ex_oppNameAr),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oppNameEnColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oppNameEnNull())? exportRow.Ex_oppNameEn : null),Ex_oppNameEn),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_opprtrquntColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_opprtrqunt,Ex_opprtrqunt),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtdateColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_oprtdate,Ex_oprtdate),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtofficomentColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtofficomentNull())? exportRow.Ex_oprtofficoment : null),Ex_oprtofficoment),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtpaymentColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtpaymentNull())? exportRow.Ex_oprtpayment : null) ,Ex_oprtpayment),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtshippColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtshippNull())? exportRow.Ex_oprtshipp :null) ,Ex_oprtshipp),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtspecifColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_oprtspecif,Ex_oprtspecif),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_pathColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_pathNull())? exportRow.Ex_path :null),Ex_path),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_ProdDescColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_ProdDescNull())? exportRow.Ex_ProdDesc : null),Ex_ProdDesc),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_ProdhowtoregColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_ProdhowtoregNull())? exportRow.Ex_Prodhowtoreg : null),Ex_Prodhowtoreg),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Wcomp_idColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Wcomp_id,Wcomp_id),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.ProdCat_IdColumn.ColumnName,exportRow.Ex_oprtid,exportRow.ProdCat_Id,ProdCat_Id),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.PublishingColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Publishing,Publishing),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.FormemberonlyColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Formemberonly,Formemberonly)
                    }
              );

            #endregion

            if (string.IsNullOrEmpty(Ex_oppNameEn )) exportRow.SetEx_oppNameEnNull(); else exportRow.Ex_oppNameEn = Ex_oppNameEn;
            if (string.IsNullOrEmpty(Ex_oppNameAr )) exportRow.SetEx_oppNameArNull(); else exportRow.Ex_oppNameAr = Ex_oppNameAr;
            if (string.IsNullOrEmpty(Ex_ProdDesc )) exportRow.SetEx_ProdDescNull(); else exportRow.Ex_ProdDesc = Ex_ProdDesc;            
            if (string.IsNullOrEmpty(Ex_Prodhowtoreg )) exportRow.SetEx_ProdhowtoregNull(); else exportRow.Ex_Prodhowtoreg = Ex_Prodhowtoreg;
            if (string.IsNullOrEmpty(Ex_path )) exportRow.SetEx_pathNull(); else exportRow.Ex_path = Ex_path;
            if (string.IsNullOrEmpty(Ex_oprtpayment)) exportRow.SetEx_oprtpaymentNull(); else exportRow.Ex_oprtpayment = Ex_oprtpayment;
            if (string.IsNullOrEmpty(Ex_oprtshipp)) exportRow.SetEx_oprtshippNull(); else exportRow.Ex_oprtshipp = Ex_oprtshipp;
            if (string.IsNullOrEmpty(Ex_oprtofficoment)) exportRow.SetEx_oprtofficomentNull(); else exportRow.Ex_oprtofficoment = Ex_oprtofficoment;

            if (string.IsNullOrEmpty(Ex_path)) exportRow.SetEx_pathNull(); else exportRow.Ex_path = Ex_path;
            if (Publishing == null) exportRow.SetPublishingNull(); else exportRow.Publishing = Publishing.Value;
            
    
            exportRow.Ex_oprtdate = Ex_oprtdate;
            exportRow.Ex_opprtrqunt = Ex_opprtrqunt;
            exportRow.Ex_oprtspecif = Ex_oprtspecif;                        
            exportRow.Wcomp_id = Wcomp_id;
            exportRow.Formemberonly = Formemberonly;
            exportRow.ProdCat_Id = ProdCat_Id;

            int roweffected = ExpOppAdapter.Update(exportRow);
            return roweffected == 1;

        }


        #endregion

        #region DeleteExportOpp

        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteExportOpp(int Ex_oprtid)
        {
            EcsSiteDS.ExpOpprtintyDataTable exportTable = ExpOppAdapter.GetExpOpprtintyDataByExoprtId(Ex_oprtid);
            if (exportTable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.ExpOpprtintyRow exportRow = exportTable[0];
            if (!exportRow.IsEx_pathNull())
            {
                System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                         + exportRow.Ex_path));
            }

            #region LogActivity
            Helper.LogActivity("Delete from ExportOpp table", ActivityOperation.Deleted, true,
              new LogActivityInfo[] 
                    { 
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oppNameArColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oppNameArNull())? exportRow.Ex_oppNameAr : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oppNameEnColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oppNameEnNull())? exportRow.Ex_oppNameEn : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_opprtrquntColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_opprtrqunt,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtdateColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_oprtdate,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtofficomentColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtofficomentNull())? exportRow.Ex_oprtofficoment : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtpaymentColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtpaymentNull())? exportRow.Ex_oprtpayment : null) ,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtshippColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_oprtshippNull())? exportRow.Ex_oprtshipp : null) ,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_oprtspecifColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Ex_oprtspecif,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_pathColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_pathNull())? exportRow.Ex_path : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_ProdDescColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_ProdDescNull())? exportRow.Ex_ProdDesc : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Ex_ProdhowtoregColumn.ColumnName,exportRow.Ex_oprtid,(!(exportRow.IsEx_ProdhowtoregNull())? exportRow.Ex_Prodhowtoreg : null),null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.Wcomp_idColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Wcomp_id,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.ProdCat_IdColumn.ColumnName,exportRow.Ex_oprtid,exportRow.ProdCat_Id,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.PublishingColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Publishing,null),
                    new LogActivityInfo(exportTable.TableName,
                    exportTable.FormemberonlyColumn.ColumnName,exportRow.Ex_oprtid,exportRow.Formemberonly,null)
                    }
              ); 

            #endregion
          
            new A2UControl.WebCustomEvents.RecordDeletedEvent("Export Opportunity", Ex_oprtid, null).Raise();
            int rowsAffected = ExpOppAdapter.Delete(Ex_oprtid);
            return rowsAffected == 1;
        }
        #endregion

    }

}